﻿Imports System.Collections.Specialized
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports System.Windows.Media.Animation
Imports System.Windows.Threading
Imports System.IO
Imports System.Net
Imports System.Threading

Class MainWindow

    Implements Controlers.ITeamSystemView(Of MainWindow)

    Implements Controlers.IHeatView(Of MainWindow)

    Implements Controlers.ILinksView(Of MainWindow)

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        Try
            InitializeComponent()
        Catch ex As Exception
            Dim x As Integer = 1
        End Try
       
    End Sub

    Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Dim tsc As New Controlers.TeamSystemControler(Of MainWindow)(Me)
        Application.ControlerFactory.AddControler(tsc)
        Dim hc As New Controlers.HeatControler(Of MainWindow)(Me)
        Application.ControlerFactory.AddControler(hc)
        Dim lc As New Controlers.LinksControler(Of MainWindow)(Me)
        Application.ControlerFactory.AddControler(lc)
        '----------------
        ShowWelcomeScreen()
        '--------------------------
        ' Set TFS bits
        uxSelectTeamServer.DataContext = TeamSystemControler
        uxSelectTeamProject.DataContext = TeamSystemControler
        uxTeamSystemStatus.DataContext = TeamSystemControler
        uxSelectWorkItemType.DataContext = TeamSystemControler
        ' Set Heat bits
        uxSelectHeatApplication.DataContext = HeatControler
        uxHeatStatus.DataContext = HeatControler
        ' Set Links Bit
        uxHeatList.DataContext = LinksControler
        uxLinkStatus.DataContext = LinksControler
    End Sub

    ''' <summary>
    ''' Command handler for changing the selected team project
    ''' </summary>
    Private Sub CodeplexCommand_Executed(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
        Dim Param As String = e.Parameter
        Dim nav As String = "http://hinshelwood.com/TFSHeatITSM.aspx"
        Select Case Param
            Case "Home"
                nav = "http://hinshelwood.com/TFSHeatITSM.aspx"
            Case "Release"
                nav = "http://www.codeplex.com/TFSHeatITSM/Release/ProjectReleases.aspx"
            Case "Discussions"
                nav = "http://www.codeplex.com/TFSHeatITSM/Thread/List.aspx"
            Case "IssueTracker"
                nav = "http://www.codeplex.com/TFSHeatITSM/WorkItem/AdvancedList.aspx"
            Case "SourceCode"
                nav = "http://www.codeplex.com/TFSHeatITSM/SourceControl/ListDownloadableCommits.aspx"
            Case "License"
                nav = "http://www.codeplex.com/TFSHeatITSM/license"
            Case "Martin"
                nav = "http://blog.hinshelwood.com"
            Case Else
                nav = "http://hinshelwood.com"
        End Select
        System.Diagnostics.Process.Start(nav)
    End Sub


    Private Sub ShowAboutCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        uxAbout.Visibility = Windows.Visibility.Visible
    End Sub

    Private Sub ClearSettingsCommand_Executed(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
        My.Settings.Reset()
        My.Settings.Reload()
        TeamSystemControler.Reset()
        HeatControler.Reset()
        LinksControler.Reset()
    End Sub

    ''' <summary>
    ''' Command handler for logo click event
    ''' </summary>
    Private Sub RDdotNet_MouseLeftButtonUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        ' Open the RDdotNet website in the user's default browser
        System.Diagnostics.Process.Start("http://hinshelwood.com/TFSHeatITSM.aspx")
    End Sub

    ''' <summary>
    ''' Show the Welcome Screen user control and hides the other controls.
    ''' </summary>
    Friend Sub ShowWelcomeScreen()
        'DiagramControl.Visibility = Visibility.Hidden
        'WelcomeUserControl.Visibility = Visibility.Visible
    End Sub


    '' <summary>
    '' Hides the Welcome Screen.
    '' </summary>
    Friend Sub HideWelcomeScreen()
        'WelcomeUserControl.Visibility = Visibility.Hidden

    End Sub

    'Private Sub RefreshData()

    '    Application.ItemWrappers.Clear()
    '    If Not Application.SelectedProject Is Nothing And Not Application.HeatITSM Is Nothing Then
    '        Dim v = From x In Application.HeatITSM.CallLogs Where x.Application = "BI" And Not x.CallStatus = "Closed" Take 100
    '        For Each i In v
    '            ItemWrappers.Add(New HeatWorkItem(i))
    '        Next
    '    Else
    '        'MsgBox("No Project has been selected!")
    '    End If

    'End Sub

#Region " SearchText "

    Private Sub SearchText_MouseEnter(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles SearchText.MouseEnter
        If SearchText.Text = "[enter search]" Then
            SearchText.Text = ""
            SearchPanel.Opacity = 1
        End If
    End Sub

    Private Sub SearchText_MouseLeave(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles SearchText.MouseLeave
        If SearchText.Text = "[enter search]" Or SearchText.Text = "" Then
            SearchText.Text = "[enter search]"
            SearchPanel.Opacity = 0.5
        End If
    End Sub

#End Region

#Region " ITeamSystemView "

    Public Function GetTeamFoundationServer(ByVal TeamServerName As String) As Microsoft.TeamFoundation.Client.TeamFoundationServer Implements Controlers.ITeamSystemView(Of MainWindow).GetTeamFoundationServer
        Return New TeamFoundationServer(TeamServerName, uxCredentialProviderBox)

    End Function

    Public Sub TeamServerUpdated(ByVal TeamServer As Microsoft.TeamFoundation.Client.TeamFoundationServer) Implements Controlers.ITeamSystemView(Of MainWindow).TeamServerUpdated
        If TeamServer.HasAuthenticated Then
            uxTSMenu.IsEnabled = True
        Else
            ' Fire TFS Select command
            Dim command As ICommand = Controlers.TeamSystemCommands.SelectServerCommand
            command.Execute(Nothing)
        End If
    End Sub

    Public ReadOnly Property TeamSystemControler() As Controlers.TeamSystemControler(Of MainWindow) Implements Controlers.ITeamSystemView(Of MainWindow).TeamSystemControler
        Get
            Return Application.ControlerFactory.GetControler(Of Controlers.TeamSystemControler(Of MainWindow))()
        End Get
    End Property

    Private Sub SelectTeamServerCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        uxSelectTeamServer.Visibility = Windows.Visibility.Visible
        uxTSMenu.IsEnabled = False
    End Sub

    Private Sub ChangeTeamServerCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        uxSelectTeamServer.Visibility = Windows.Visibility.Hidden
        ' Set Selected Server Name on team server
        Dim tfs As String = e.Parameter
        If Not tfs Is Nothing Then
            TeamSystemControler.UpdateSelectedServer_Async(CStr(e.Parameter))
        Else
            uxTSMenu.IsEnabled = True
        End If
    End Sub

    Private Sub SelectTeamProjectCommand_CanExecuted(ByVal sender As System.Object, ByVal e As System.Windows.Input.CanExecuteRoutedEventArgs)
        e.CanExecute = TeamSystemControler.HasSelectedServer
    End Sub

    Private Sub SelectTeamProjectCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        uxSelectTeamProject.Visibility = Windows.Visibility.Visible
        uxTSMenu.IsEnabled = False
    End Sub

    Private Sub ChangeTeamProjectCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        uxSelectTeamProject.Visibility = Windows.Visibility.Hidden
        uxTSMenu.IsEnabled = True
        Dim project As Project = e.Parameter
        If Not project Is Nothing Then
            TeamSystemControler.UpdateSelectedProject(project.Name)
        End If
    End Sub

    Private Sub SelectWorkItemTypeCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        uxSelectWorkItemType.Visibility = Windows.Visibility.Visible
    End Sub

    Private Sub ChangeWorkItemTypeCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        uxSelectWorkItemType.Visibility = Windows.Visibility.Hidden
        Dim WorkItemType As WorkItemType = e.Parameter
        If Not WorkItemType Is Nothing Then
            If TeamSystemControler.CheckWorkItemField(WorkItemType) Then
                'MsgBox("Now save work item.")
                Dim wi As WorkItem = WorkItemType.NewWorkItem
                wi.Title = String.Format("{0} {1} {2} from Heat {3}", LinksControler.SelectedItem.CallLog.Application, LinksControler.SelectedItem.CallLog.CallType, LinksControler.SelectedItem.CallLog.TypeOfCall, LinksControler.SelectedItem.CallLog.CallID)
                wi.Description = String.Format(LinksControler.SelectedItem.CallLog.CallDesc)
                TeamSystemControler.GetWorkItemField(wi, "HeatITSM.Ref").Value = CInt(LinksControler.SelectedItem.CallLog.CallID)
                ' Start Work Item Type Selection
                Dim command As ICommand = Controlers.TeamSystemCommands.CreateWorkItemCommand
                command.Execute(wi)
            Else
                MsgBox(String.Format("You will need to add the 'HeatITSM.RefNo' field to your '{0}' on project '{1}'. Use the help menu to access the documentation.", WorkItemType.Name, TeamSystemControler.SelectedProject.Name))
            End If
        End If
    End Sub

    Private Sub CreateWorkItemCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        Dim wi As WorkItem = e.Parameter
        If Not wi Is Nothing Then
            Me.uxCreateWorkItem.DataContext = wi
            Me.uxCreateWorkItem.Visibility = Windows.Visibility.Visible
        Else
            Throw New Exception("?")
        End If
      

    End Sub

    Private Sub SaveWorkItemCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        Dim wi As WorkItem = e.Parameter
        uxCreateWorkItem.Visibility = Windows.Visibility.Hidden
        If Not wi Is Nothing Then
            wi.Save()
            LinksControler.SelectedItem.LoadWorkItem()
        End If
    End Sub

#End Region

#Region " IHeatView "

    Public ReadOnly Property HeatControler() As Controlers.HeatControler(Of MainWindow) Implements Controlers.IHeatView(Of MainWindow).HeatControler
        Get
            Return Application.ControlerFactory.GetControler(Of Controlers.HeatControler(Of MainWindow))()
        End Get
    End Property

    Private Sub SelectConnectionCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        uxSelectHeatConnection.Visibility = Windows.Visibility.Visible
        uxHITSMMenu.IsEnabled = False
    End Sub

    Private Sub ChangeConnectionCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        uxSelectHeatConnection.Visibility = Windows.Visibility.Hidden
        uxHITSMMenu.IsEnabled = True

        Select Case CStr(e.Parameter)
            Case "OK"
                If Not String.IsNullOrEmpty(My.Settings.HeatCatalog) And Not String.IsNullOrEmpty(My.Settings.HeatDataSource) Then
                    Try
                        HeatControler.Reset()
                    Catch ex As Exception
                        MsgBox(ex.ToString)
                        Dim command As ICommand = Controlers.HeatCommands.SelectConnectionCommand
                        command.Execute(Nothing)
                    End Try
                End If
            Case Else
                ' Do nothing
        End Select
    End Sub

    Private Sub SelectApplicationCommand_CanExecuted(ByVal sender As System.Object, ByVal e As System.Windows.Input.CanExecuteRoutedEventArgs)
        e.CanExecute = HeatControler.IsConnected
    End Sub

    Private Sub SelectApplicationCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        uxSelectHeatApplication.Visibility = Windows.Visibility.Visible
        uxHITSMMenu.IsEnabled = False
    End Sub

    Private Sub ChangeApplicationCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        uxSelectHeatApplication.Visibility = Windows.Visibility.Hidden
        uxHITSMMenu.IsEnabled = True
        Dim app As String = e.Parameter
        If Not String.IsNullOrEmpty(app) Then
            HeatControler.SelectedApplication = app
            LinksControler.Reset()
        End If
   
    End Sub

#End Region

#Region " ILinksView "

    Public ReadOnly Property LinksControler() As Controlers.LinksControler(Of MainWindow) Implements Controlers.ILinksView(Of MainWindow).LinksControler
        Get
            Return Application.ControlerFactory.GetControler(Of Controlers.LinksControler(Of MainWindow))()
        End Get
    End Property


    Private Sub UpdateCallLogRefNoWithTFSId_Executed(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
        Dim LinkItem As Controlers.LinkItem(Of MainWindow) = e.Parameter
        MsgBox(String.Format("Should now update Call {0} with TFS WI {1}", LinkItem.CallLog.CallID, LinkItem.WorkItem.Id))
    End Sub

    Private Sub ChangeItemCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        Dim LinkItem As Controlers.LinkItem(Of MainWindow) = e.Parameter
        LinksControler.SelectedItem = LinkItem
        If Not TeamSystemControler.CheckServerField Then
            MsgBox("You will need to add the 'HeatITSM.RefNo' field to your team foundation server work item types. Use the help menu to access the documentation.")
            Exit Sub
        End If
        If Not TeamSystemControler.HasSelectedProject Then
            MsgBox("You will need to Select a project first.")
            Exit Sub
        End If
        ' Start Work Item Type Selection
        Dim command As ICommand = Controlers.TeamSystemCommands.SelectWorkItemTypeCommand
        command.Execute(Nothing)
    End Sub

#End Region



 
 


End Class
